<template>
  <div>
    <el-form ref="form">
      <el-form-item prop="search" class="mb20 mt10">
        <el-col :span="4">
          <el-button type="primary" @click="handleAdd" icon="el-icon-circle-plus-outline">添加订单</el-button>
        </el-col>
        <el-row type="flex" class="row-bg" justify="end">
          <el-col :xs="12" :sm="10" :md="8" :lg="6">
            <el-input
              v-model.number="search"
              size="mini"
              placeholder="输入客户电话或客户姓名搜索"
              @keyup.enter.native="getRecords(15, 0)"
            ></el-input>
          </el-col>
          <el-col :xs="12" :sm="10" :md="8" :lg="6">
            <div class="block">
              <el-date-picker
                v-model="visitTime"
                @change="handleVisitSearch"
                type="month"
                placeholder="选择上门日期"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </div>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="getRecords(15, 0)"
            >搜 索</el-button>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button type="success" size="mini" :icon="searchIcon" @click="advancedSearch()">高级搜索</el-button>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button type="primary" size="mini" icon="el-icon-refresh" @click="resetSearch">重 置</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <advancedSearch
      :advancedSearchs="advancedSearchs"
      v-show="showAdvancedSearch"
      @filter="handleFilter"
    ></advancedSearch>
    <recordDialog
      :title="title"
      :addDialog="addDialog"
      :editDialog="editDialog"
      :recordForm="recordForm"
      @close="hanleClose"
      @update="handleUpdate"
    ></recordDialog>
    <el-table
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column type="selection" width="45"></el-table-column>
      <el-table-column prop="id" label="序号" width="65" fixed></el-table-column>
      <el-table-column sortable prop="createdAt" label="创建时间" width="140" fixed>
        <template slot-scope="scope">{{scope.row.createdAt | moment("YYYY/MM/DD h:mm:ss")}}</template>
      </el-table-column>
      <el-table-column sortable prop="time" label="上门时间" width="95" fixed>
        <template slot-scope="scope">{{scope.row.time | moment("YYYY/MM/DD")}}</template>
      </el-table-column>
      <el-table-column
        prop="isSign"
        label="签订"
        width="75"
        fixed
        :filters="[{ text: '已签订', value: '已签订' }, { text: '未签订', value: '未签订' }]"
        :filter-method="filterTag"
      ></el-table-column>
      <el-table-column prop="name" label="客户姓名" width="70" fixed></el-table-column>
      <el-table-column prop="phone" label="客户电话" width="100" fixed></el-table-column>
      <el-table-column prop="isMortgage" label="购车类型" width="90" fixed></el-table-column>
      <el-table-column prop="promotionName" label="推广渠道" width="70"></el-table-column>
      <el-table-column prop="distributionName" label="区域" width="80"></el-table-column>
      <el-table-column prop="deposit" label="定金(元)" width="80"></el-table-column>
      <el-table-column prop="notsign" label="未签订原因" width="100"></el-table-column>
      <el-table-column prop="centerName" label="中心" width="50"></el-table-column>
      <el-table-column prop="deptName" label="部门" width="50"></el-table-column>
      <el-table-column prop="salesmanName" label="业务员" width="70"></el-table-column>
      <el-table-column prop="operation" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
            icon="el-icon-edit-outline"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt20">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pages"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import recordDialog from "@/components/record_dialog";
import advancedSearch from "@/components/advanced_search";
import { getFilterMonthLastDay } from "@/utils/getCurrMonth";

export default {
  name: "record",
  data() {
    return {
      title: "",
      loading: true,
      tableData: [],
      addDialog: false,
      showAdvancedSearch: false,
      visitTime: "",
      page: 15,
      total: 0,
      val: 1,
      search: "",
      advanced_search: "",
      visitTimeSearch: ["", ""],
      advancedSearchs: ["sign", "deposit_to_achievement"],
      sign: "",
      depositToAchievement: "",
      currentPage: 1,
      pages: [15, 20, 25, 35],
      editDialog: false,
      recordForm: {},
      searchIcon: "el-icon-arrow-down"
    };
  },
  created() {
    this.getRecords(15, 0);
  },
  methods: {
    handleVisitSearch() {
      const time =
        this.visitTime &&
        `${this.visitTime.split("-")[0]}-${
          this.visitTime.split("-")[1]
        }-${getFilterMonthLastDay(
          this.visitTime.split("-")[0],
          this.visitTime.split("-")[1]
        )}`;
      this.visitTimeSearch = [];
      this.visitTimeSearch.push(this.visitTime ? this.visitTime : "", time);
      this.getRecords(15, 0);
    },
    advancedSearch() {
      this.searchIcon === "el-icon-arrow-down"
        ? (this.searchIcon = "el-icon-arrow-up")
        : (this.searchIcon = "el-icon-arrow-down");
      this.showAdvancedSearch = !this.showAdvancedSearch;
    },
    handleSizeChange(val) {
      this.page = val;
      this.getRecords(val, 0);
    },
    handleCurrentChange(val) {
      this.val = val;
      // 下一页页数*每页显示行-上一页页数*每页显示行
      this.getRecords(this.page, (val - 1) * this.page);
    },
    filterTag(value, row) {
      return row.isSign === value;
    },
    handleFilter(arg) {
      this.advanced_search = arg.search;
      this.sign = arg.sign;
      this.depositToAchievement = arg.depositToAchievement;
      this.getRecords(15, 0);
    },
    getRecords(limit, offset) {
      this.$axios
        .get(
          `/api/business/records/?limit=${limit}&offset=${offset}&search=${this
            .search || this.advanced_search}&sign=${
            this.sign
          }&depositToAchievement=${this.depositToAchievement}&from=${
            this.visitTimeSearch[0]
          }&to=${this.visitTimeSearch[1]}`
        )
        .then(data => {
          this.tableData = data.doc ? data.doc : [];
          this.total = data.total;
          this.loading = false;
        });
    },
    handleAdd() {
      this.addDialog = !this.addDialog;
      this.title = "添加订单";
    },
    hanleClose() {
      this.addDialog = false;
      this.editDialog = false;
      this.resetForm();
    },
    handleEdit(item) {
      this.editDialog = !this.editDialog;
      this.title = "编辑订单";
      this.recordForm = item;
    },
    resetForm() {
      this.recordForm = {
        time: "",
        promotion: "",
        deposit: "",
        phone: "",
        center: "",
        is_sign: "",
        dept: "",
        salesman: "",
        notsigning: "",
        distribution: ""
      };
    },
    resetSearch() {
      this.search = "";
      this.getRecords(15, 0);
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该条订单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios.delete("/api/business/removeRecord/" + id).then(data => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.handleUpdate();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleUpdate() {
      this.getRecords(this.page, (this.val - 1) * this.page);
    }
  },
  components: {
    recordDialog,
    advancedSearch
  }
};
</script>
<style>
</style>
